package com.palnar.ilecker.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

public class Menu extends ItemBase implements Serializable {

	private Integer clientId;
	private Integer serverId;
	private Boolean published;
	private Integer publisher;
	private String publishedOn;
	private String name;
	private Boolean expired;
	private Integer sepId;
	private List<ItemGroup> itemGroupList;
	private ArrayList<Integer> serviceEndPoints;

	public Menu() {
		itemGroupList = new ArrayList<ItemGroup>();
	}

	public Integer getClientId()
	{
	 return clientId;
	}
	
	public Integer getServerId()
	{
		return serverId;
	}

	public Boolean getIsPublished()
	{
		return published;
	}
	
	public Integer getPublisher()
	{
		return this.publisher;
	}
	
	public String getPublishedOn()
	{
		return this.publishedOn;
	}
	
//	public String getName()
//	{
//		return this.name;
//	}
	
	public Boolean getIsExpired()
	{
		return this.expired;
	}

//	public Integer getSEPId()
//	{
//		return this.sepId;
//	}

	public List<ItemGroup> GetItemGroupList() {
		return this.itemGroupList;
	}

	public void setClientId(Integer clientId)
	{
	 this.clientId= clientId;
	}
	
	public void setServerID(Integer serverId)
	{
		this.serverId= serverId;
	}

	public void setIsPublished(Boolean published)
	{
		this.published= published;
	}
	
	public void setPublisher(Integer publisher)
	{
		this.publisher= publisher;
	}
	
	public void setPublishedOn(String publishedOn)
	{
		this.publishedOn =publishedOn;
	}
		
//	public void setName(String name)
//	{
//		this.name = name;
//	}
	
	public void getIsExpired(Boolean expired)
	{
		this.expired = expired;
	}		

//	public void setSEPId(Integer id)
//	{
//		this.sepId= id;
//	}

	public void setItemGroupList(List<ItemGroup> itemGroupList) {
		for (ItemGroup itemGroup : itemGroupList) 
		{
			this.itemGroupList.add(itemGroup);
		}
	}
	
	public ItemGroup TryGetItemGroup(Integer id)
	{		
		if(!this.itemGroupList.isEmpty())
		{
			for (ItemGroup itemGroup : this.itemGroupList) 
			{
				if(itemGroup.getClientId()== id)
				{
				 return itemGroup; 
				}
			}
		}
		return null;
	}
	
	public static Menu MenuGroupBuilder(SoapObject outputSoapObject, HashMap<Integer, Language> languages) 
	{
        Menu menu = new Menu();
        Integer id = Integer.parseInt(outputSoapObject.getProperty("Id").toString());
//        String version = outputSoapObject.getProperty("Version").toString();
//        String availabilityVersion = outputSoapObject.getProperty("ItemAvailableVersion").toString();
        menu.setServerID(id);
        menu.expired = Boolean.parseBoolean(outputSoapObject.getProperty("Expired").toString());
//        menu.setVersion(version);
//        menu.setItemAvailableVersion(availabilityVersion);
		SoapObject itemGroupSoapObject = (SoapObject) outputSoapObject.getProperty("Children");
		List<ItemGroup> itemGroupList = new ArrayList<ItemGroup>();
		
		for (int i = 0; i < itemGroupSoapObject.getPropertyCount(); i++) 
		{
			ItemGroup itemGroup = new ItemGroup();			
			SoapObject itemGroupSoapObject2 = (SoapObject) itemGroupSoapObject.getProperty(i);
			itemGroup = ItemGroup.ItemGroupBuilder(itemGroupSoapObject2, languages);
			itemGroupList.add(itemGroup);
		
		}
				

		SoapObject sepSoapObject = (SoapObject) outputSoapObject.getProperty("ServiceEndPoints");
		menu.setServiceEndPoints(new ArrayList<Integer>());
		
		for (int i = 0; i < sepSoapObject.getPropertyCount(); i++) 
		{			
			Integer sepId = Integer.parseInt(sepSoapObject.getProperty(i).toString());
			menu.getServiceEndPoints().add(sepId);
		}

		menu.itemGroupList =itemGroupList;
		return menu;
	}

	public ArrayList<Integer> getServiceEndPoints() {
		return serviceEndPoints;
	}

	public void setServiceEndPoints(ArrayList<Integer> serviceEndPoints) {
		this.serviceEndPoints = serviceEndPoints;
	}

}
